<?php

namespace App\Http\Controllers\Home;

use App\lib\Cart;
use App\Model\Goods;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CartController extends CommonController
{
    //购物车
    public function index(Cart $cart)
    {
        $cartDatas = $cart->getAllData();
        return view('home.cart.index', compact('cartDatas'));
    }

    //添加购物车
    public function addcart(Cart $cart)
    {
        //组装购物车数据
        $indata = request();

        //可以验证库存 以及处理库存等逻辑

        $data['id'] = $indata['id'];
        $data['name'] = $indata['name'];
        $data['num'] = 1;
        $data['price'] = Goods::find($indata['id'])->shop_price;
        $data['options'] = [
            'title' => $indata['title'],
            'lists' => $indata['lists'],
            'imgs' => Goods::find($indata['id'])->list_img
        ];
        $cart->add($data);
        return 'ok';
    }

    //更新购物车
    public function upcart(Cart $cart)
    {
        $cart->update(request());
        return 'ok';
    }

    //删除购物车
    public function delcart(Cart $cart)
    {
        $cart->del($indata = request('sid'));
        return 'ok';
    }

}
